package com.example.aboutwork.swagger;

import com.fasterxml.classmate.ResolvedType;
import com.google.common.base.Optional;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;
import springfox.documentation.builders.ModelPropertyBuilder;
import springfox.documentation.schema.Annotations;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.schema.ModelPropertyBuilderPlugin;
import springfox.documentation.spi.schema.contexts.ModelPropertyContext;
import springfox.documentation.swagger.schema.ApiModelProperties;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 自定义swagger有效注解
 *
 * @author lixing
 * @version V1.0 2021/4/12 10:43 上午
 **/
@Component
@Primary
@Slf4j
public class SwaggerDisplayConfig implements ModelPropertyBuilderPlugin {


    @Override
    public void apply(ModelPropertyContext context) {
        //获取当前字段的类型
        final Class fieldType = context.getBeanPropertyDefinition().get().getField().getRawType();

        //为枚举字段设置注释
        descForEnumFields(context, fieldType);
    }

    /**
     * 为枚举字段设置注释
     */
    private void descForEnumFields(ModelPropertyContext context, Class fieldType) {
        Optional<ApiModelProperty> annotation = Optional.absent();

//        if (context.getAnnotatedElement().isPresent()) {
//            annotation = annotation.or(ApiModelProperties.findApiModePropertyAnnotation(context.getAnnotatedElement().get()));
//        }
//        if (context.getBeanPropertyDefinition().isPresent()) {
//            annotation = annotation.or(Annotations.findPropertyAnnotation(context.getBeanPropertyDefinition().get(),
//                    ApiModelProperty.class));
//        }

        //没有@ApiModelProperty 或者 notes 属性没有值，直接返回
        if (!annotation.isPresent() || StringUtils.isBlank((annotation.get()).notes())) {
            return;
        }

        //@ApiModelProperties中的notes指定的class类型
        Class rawPrimaryType;
        try {
            rawPrimaryType = Class.forName((annotation.get()).notes());
        } catch (ClassNotFoundException e) {
            //如果指定的类型无法转化，直接忽略
            return;
        }

        //如果对应的class是一个@SwaggerDisplayEnum修饰的枚举类，获取其中的枚举值
        Object[] subItemRecords = null;
        SwaggerDisplayEnum swaggerDisplayEnum = AnnotationUtils
                .findAnnotation(rawPrimaryType, SwaggerDisplayEnum.class);
        if (null != swaggerDisplayEnum && Enum.class.isAssignableFrom(rawPrimaryType)) {
            subItemRecords = rawPrimaryType.getEnumConstants();
        }
        if (null == subItemRecords) {
            return;
        }


       String joinText = Arrays.stream(subItemRecords).filter(Objects::nonNull).map(item -> {
            String typeName = swaggerDisplayEnum.type();
            String descName = swaggerDisplayEnum.desc();

            Object type = null;
            Object desc = null;

            Field typeField = ReflectionUtils.findField(item.getClass(), typeName);
            if (typeField != null) {
                ReflectionUtils.makeAccessible(typeField);
                type = ReflectionUtils.getField(typeField, item);
            }

            Field descField = ReflectionUtils.findField(item.getClass(), descName);
            if (descField != null) {
                ReflectionUtils.makeAccessible(descField);
                desc = ReflectionUtils.getField(descField, item);
            }
            type = type == null ? "" : type;
            desc = desc == null ? "" : desc;
            return type + ":" + desc;
        }).collect(Collectors.joining(";","(",")"));

        try {
            Field mField = ModelPropertyBuilder.class.getDeclaredField("description");
            mField.setAccessible(true);
            joinText = mField.get(context.getBuilder()) + joinText;
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        final ResolvedType resolvedType = context.getResolver().resolve(fieldType);
        context.getBuilder().description(joinText).type(resolvedType);
    }


    @Override
    public boolean supports(DocumentationType documentationType) {
        return true;
    }

}
